﻿using ABI.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using VirtualDesktop.Interfaces;

namespace WindowsDesktop.API
{

    /// <summary>
    /// 特殊说明一下：分为两部分是因为客户使用的时候不希望一开始初始化
    /// 而菜单需要注册事件需要一开始初始化
    /// </summary>
    public partial class AppServer : IAppServer
    {
        static readonly object Lock = new object();
        static AppServer Instance = null;
        static bool InitEd = false;
        /// <summary>
        /// 工作桌面标题
        /// </summary>
        public static string iHubWindowsTitle { set; get; }

        static void CheckInit()
        {
            if (InitEd) return;
            else
            {
                var version = GetStaticInstance().VersionDic.Where(v => v.Value).First();
                //if (version == null) return;
                switch (version.Key)
                {
                    case Vervsions.Win10:
                        Win10.VirtualDesktop.DesktopManager.CurrentChanged += (o,e)=> 
                        {
                            CurrentChanged?.Invoke(e.NewDesktop.GetId() == GetIhubDesktop()?.Guid);
                        };
                        break;
                    case Vervsions.Win11:
                        Win11.VirtualDesktop.DesktopManager.CurrentChanged += (o, e) =>
                        {
                            CurrentChanged?.Invoke(e.NewDesktop.GetId() == GetIhubDesktop()?.Guid);
                        };
                        break;
                    case Vervsions.Win11_21H2:
                        Win11_21H2.VirtualDesktop.DesktopManager.CurrentChanged += (o, e) =>
                        {
                            CurrentChanged?.Invoke(e.NewDesktop.GetId() == GetIhubDesktop()?.Guid);
                        };
                        break;
                    case Vervsions.Win11_23H2:
                        Win11_23H2.VirtualDesktop.DesktopManager.CurrentChanged += (o, e) =>
                        {
                            CurrentChanged?.Invoke(e.NewDesktop.GetId() == GetIhubDesktop()?.Guid);
                        };
                        break;
                    case Vervsions.Win11_23H2_3085:
                        Win11_23H2_3085.VirtualDesktop.DesktopManager.CurrentChanged += (o, e) =>
                        {
                            CurrentChanged?.Invoke(e.NewDesktop.GetId() == GetIhubDesktop()?.Guid);
                        };
                        break;
                    case Vervsions.Win11_24H2:
                        Win11_24H2.VirtualDesktop.DesktopManager.CurrentChanged += (o, e) =>
                        {
                            CurrentChanged?.Invoke(e.NewDesktop.GetId() == GetIhubDesktop()?.Guid);
                        };
                        break;
                    default:
                        break;
                }
            }
        }

        public static void Reset()
        {
            var version = GetStaticInstance().VersionDic.Where(v => v.Value).First();
            //if (version == null) return;
            switch (version.Key)
            {
                case Vervsions.Win10:
                    Win10.VirtualDesktop.DesktopManager.ResetDesktopManager();
                    break;
                case Vervsions.Win11:
                    Win11.VirtualDesktop.DesktopManager.ResetDesktopManager();  
                    break;
                case Vervsions.Win11_21H2:
                    Win11_21H2.VirtualDesktop.DesktopManager.ResetDesktopManager();
                    break;
                case Vervsions.Win11_23H2:
                    Win11_23H2.VirtualDesktop.DesktopManager.ResetDesktopManager();
                    break;
                case Vervsions.Win11_23H2_3085:
                    Win11_23H2_3085.VirtualDesktop.DesktopManager.ResetDesktopManager();
                    break;
                case Vervsions.Win11_24H2:
                    Win11_24H2.VirtualDesktop.DesktopManager.ResetDesktopManager();
                    break;
                default:
                    break;
            }
        }

        static AppServer GetStaticInstance()
        {
            if (Instance == null)
            {
                lock (Lock)
                {
                    if(Instance == null)
                        Instance = new AppServer();
                }
            }
            return Instance;
        }

        static Action<bool> CurrentChanged;
        /// <summary>
        /// 注册虚拟桌面切换事件
        /// </summary>
        /// <param name="changedEvent">如果目标是iHub所在桌面为true,否则为false</param>
        public static void RegisterDeskTopChangeEvennt(Action<bool> changedEvent)
        {
            CheckInit();
            CurrentChanged += changedEvent;
        }
        public static int CheckWinodw()
        {
            IntPtr hWnd = FindWindow(null, iHubWindowsTitle);
            if (!IsWindow(hWnd))
                return -1;
            // 检查窗口是否为顶级窗口
            if (GetParent(hWnd) !=IntPtr.Zero  ||  (GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD) != 0)
            {
                return -2;
            }
            return 1;

        }
        public static IDesktop GetIhubDesktop()
        {
            IntPtr hWnd = FindWindow(null, iHubWindowsTitle);
            if (hWnd == IntPtr.Zero)
                return null;
            return GetStaticInstance().FromHwnd(hWnd);
        }
        #region WinAPI
        // 导入FindWindow函数
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        // 导入 IsWindow、GetParent 和 GetWindowLong 函数
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool IsWindow(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetParent(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        private const int GWL_STYLE = -16;
        private const int WS_CHILD = 0x40000000;
        #endregion
    }
}
